import org.junit.jupiter.api.Test;

/**
 * 多线程的常用方法
 *
 * @author 阳秋豪
 * @注意 我这都用的测试方法是为了方便观看，最好运行时改成main方法否者多线程会出现一堆无可预测的问题
 * @createTime 2024/3/11
 */
public class Test3 {
    @Test
    public void test1() throws InterruptedException {
        // 1、public final synchronized void setName(String name)
        // -- 设置线程的名字(构造方法也可以设置名字)
        // -- 如果我们没有给线程设置名字，线程也是有默认的名字的格式:Thread-X(X序号，从0开始的)
        // 2、public final String getName()
        // -- 返回此线程的名称
        Thread t2 = new Thread("defaultName");
        System.out.println(t2.getName()); // defaultName
        t2.setName("updateName");
        System.out.println(t2.getName()); // updateName

        // 3、public static native Thread currentThread()
        // -- 获取当前线程的对象
        Thread currentThread = Thread.currentThread();
        System.out.println(currentThread.getName()); // main

        // 4、public static native void sleep(long millis) throws InterruptedException
        // -- 让线程休眠指定的时间，单位为毫秒
        System.out.println("..................");
        Thread.sleep(5000);
        System.out.println("..................");
    }

    @Test
    public void test2() {
        Test3MyThread1 t1 = new Test3MyThread1();
        Test3MyThread1 t2 = new Test3MyThread1();
        t1.setName("线程1");
        t2.setName("线程2");
        // 5、public final int getPriority()
        // -- 获取线程优先级
        System.out.println(t1.getPriority()); // 5
        System.out.println(t2.getPriority()); // 5
        // 6、public final void setPriority(int newPriority)
        // -- 设置线程优先级
        // -- 当其他的非守护线程执行完毕之后，守护线程会陆续结束
        t1.setPriority(1);
        t2.setPriority(10);
        System.out.println(t1.getPriority()); // 1
        System.out.println(t2.getPriority()); // 10
        t1.start();
        t2.start();
    }

    @Test
    public void test3() {
        Test3MyThread1 t1 = new Test3MyThread1();
        Test3MyThread2 t2 = new Test3MyThread2();
        t1.setName("主线程");
        t2.setName("守护线程");
        // 7、public final void setDaemon(boolean on)
        // -- 开启守护线程
        // -- 当其他的非守护线程执行完毕之后，守护线程会陆续结束
        t2.setDaemon(true);
        t1.start();
        t2.start();
    }

    @Test
    public void test4() {
        Test3MyThread1 t1 = new Test3MyThread1();
        // 8、public static native void yield()
        // -- 出让线程/礼让线程
        Test3MyThread3 t3 = new Test3MyThread3();
        t1.setName("线程1");
        t3.setName("线程3");
        t1.start();
        t3.start();
    }

    @Test
    public void test5() throws InterruptedException {
        Test3MyThread1 t1 = new Test3MyThread1();
        t1.setName("线程1");
        t1.start();
        // 9、public final void join() throws InterruptedException
        // -- 插入线程
        t1.join();
        for (int i = 1; i <= 10; i++) {
            System.out.println("main" + i);
        }
    }
}

class Test3MyThread1 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(getName() + " --------- @" + (i + 1) + "线程");
        }
    }
}

class Test3MyThread2 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(getName() + " --------- @" + (i + 1) + "线程");
        }
    }
}

class Test3MyThread3 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(getName() + " --------- @" + (i + 1) + "线程");
            Thread.yield();
        }
    }
}

